package com.duobaoyu.dby.supply.biz.service.config.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.common.mybatis.support.ParamConvert;
import com.duobaoyu.dby.supply.biz.service.config.SystemLogService;
import com.duobaoyu.dby.supply.common.biz.dto.config.SystemLogDTO;
import com.duobaoyu.dby.supply.db.entity.message.SystemLog;
import com.duobaoyu.dby.supply.db.repository.message.SystemLogRepository;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName SystemLogServiceImpl
 * @createTime 15:24:00 2023/10/16
 */
@Service
@RequiredArgsConstructor
public class SystemLogServiceImpl implements SystemLogService {

    private final SystemLogRepository systemLogRepository;

    @Override
    public SystemLog getById(String systemLogId) {
        return systemLogRepository.selectOneById(systemLogId);
    }

    @Override
    public Page<SystemLog> page(SystemLogDTO systemLog) {
        Criteria criteria = new Criteria();
        //模糊查询
        if(StringUtils.isNotBlank(systemLog.getOperationName())) {
            criteria.and("operation_name").regex(Pattern.compile("^.*" + systemLog.getOperationName() + ".*$", Pattern.CASE_INSENSITIVE));
        }
        if(StringUtils.isNotBlank(systemLog.getRequestId())) {
            criteria.and("request_id").is(systemLog.getRequestId());
        }
        if(StringUtils.isNotBlank(systemLog.getHttpMethod())) {
            criteria.and("http_method").is(systemLog.getHttpMethod());
        }
		if(StringUtils.isNotBlank(systemLog.getOperationType())) {
			criteria.and("operation_type").is(systemLog.getOperationType());
		}
        if(StringUtils.isNotBlank(systemLog.getClassMethod())) {
            criteria.and("class_method").regex(Pattern.compile("^.*" + systemLog.getClassMethod() + ".*$", Pattern.CASE_INSENSITIVE));
        }
        return systemLogRepository.page(ParamConvert.getPage(systemLog), Query.query(criteria));
    }
}
